<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Content extends CI_Controller {

    function __construct()
    {
        // Call the Model constructor
        parent::__construct();
        $this->load->model('content_model');
        $template['current_class'] = "content";
    }

	public function index()
	{
	    $data['contents'] = $this->content_model->get_all(20, $this->uri->segment(4));
	    $template['content'] = $this->load->view('admin/content', $data, TRUE);
		$this->load->view('admin/template', $template);
	}
	
	public function create()
	{
	    $this->form_validation->set_rules('title','description','required');
        $this->form_validation->set_error_delimiters('<span class="form-msg-error">', '</span>');
	    $editor['title'] = "Create Content";
	    $editor['form_url'] = site_url('admin/content/create');
	    if($this->form_validation->run())
    	{
    	    $title = $this->input->post('title', TRUE);
            $description = $this->input->post('description', TRUE);
            $id = $this->content_model->create($title, $description);
            if ($id) 
            {
                $this->_upload_picture($id);
                redirect('admin/content');
            }else{
                $editor['errors'] = 'Database error please contact your administrator.';
            }
            
    	}else{
    	    
    	}
    	$template['content'] = $this->load->view('admin/content_editor', $editor, TRUE);
		$this->load->view('admin/template', $template);
    	
	}
	function edit($content_id)
    {
        $this->form_validation->set_rules('title','description','required');
        $this->form_validation->set_error_delimiters('<span class="form-msg-error">', '</span>');
	    $movie_editor['title'] = "Edit Content";
	    $movie_editor['form_url'] = site_url('admin/content/edit/'.$content_id);
	    $movie_editor['content'] = $this->content_model->get_by_id($content_id);
	    if($this->form_validation->run())
    	{
    	    $title = $this->input->post('title', TRUE);
            $description = $this->input->post('description');
            $result = $this->content_model->update($content_id,$title, $description);
            if ($result) 
            {
                $this->_upload_picture($content_id);
                redirect('admin/content');
            }else{
                $branch_editor['errors'] = 'Database error please contact your administrator.';
            }
            
    	}else{
    	    
    	}
    	$template['content'] = $this->load->view('admin/content_editor', $movie_editor, TRUE);
		$this->load->view('admin/template', $template);
    }
    
    function destroy($content_id)
    {
        $this->content_model->delete($content_id);
        redirect('admin/content');
    }
    
    function _upload_picture($content_id)
    {
        $config['upload_path'] = './uploads/content/';
        $config['allowed_types'] = 'jpg';
        $config['file_name'] ="temp" ;
        $config['overwrite'] = TRUE;
        $this->load->library('upload', $config);
        if (! $this->upload->do_upload('picture')) 
        {
            //print_r($this->upload->display_errors());
        }
        else
        {
            $config['overwrite'] = TRUE;
            $config['image_library'] = 'gd2';
            $config['source_image'] = './uploads/content/temp.jpg';
            $config['maintain_ratio'] = FALSE;
            $config['width'] = 800;
            $config['height'] = 350;
            $config['quality'] = 100;
            $config['new_image'] = "./uploads/content/".$content_id.".jpg";
            $this->load->library('image_lib', $config);
            $this->image_lib->resize();
            
            $upload = $this->upload->data();
            $config['image_library'] = 'gd2';
            $config['source_image'] = './uploads/content/temp.jpg';
            $config['maintain_ratio'] = FALSE;
            $config['width'] = 100;
            $config['height'] = 44;
            $config['quality'] = 100;
            $config['new_image'] = "./uploads/content/".$content_id."_thumb.jpg";
            $this->image_lib->clear();
            $this->image_lib->initialize($config);
            $this->image_lib->resize();
        
        }
    }
    
}